package com.app.controller;

import com.app.enums.StatusCodeEnum;
import com.app.model.JiuquanUserModel;
import com.app.model.MentorInfoModel;
import com.app.service.MentorInfoService;
import com.app.service.MentorUserService;
import com.app.service.UserService;
import com.app.util.CommonHelp;
import com.app.util.DateMaskingUtils;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.app.vo.MentorInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

//创业导师
@RestController
@Controller
@RequestMapping("/jiuquan/mentorInfo")
public class MentorInfoController {
    @Autowired
    private MentorInfoService mentorInfoService;
    @Autowired
    private UserService userService;
    //查询列表
    @GetMapping("/getMentorInfoList")
    public Result getMentorInfoList(@RequestParam(defaultValue = "0") Integer page,
                                    @RequestParam(defaultValue = "10") Integer limit, String keyword, Double longitude, Double latitude) {
        MentorInfoVo mentorInfoList = mentorInfoService.getMentorInfoList(page, limit, keyword);
        return Result.success(mentorInfoList);
    }

    //根据导师专业查询列表
    @GetMapping("/getMentorInfoMajorList")
    public Result<Object> getMentorInfoMajor(Integer page,Integer limit, String major) {
        MentorInfoVo mentorInfo = mentorInfoService.getMentorInfoMajorList(page,limit,major);
        return Result.success(mentorInfo);
    }
    //详情
    @GetMapping("/getMentorInfoById")
    public Result getMentorInfoById(Integer id) {
        MentorInfoModel mentorInfo = mentorInfoService.getById(id);
        String phone = mentorInfo.getPhone();
        String phoneMi = DateMaskingUtils.mobileEncrypt(phone);
        mentorInfo.setPhoneMi(phoneMi);
        String vx = mentorInfo.getVx();
        String vxMi = DateMaskingUtils.strMasking(vx, 2, 0);
        mentorInfo.setVxMi(vxMi);
        return Result.success(mentorInfo);
    }
    //咨询导师(辅导次数+1)
    @PostMapping("/addCounsellingCount")
    public Result addCounsellingCount(@RequestBody MentorInfoModel mentorInfoModel, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(StatusCodeEnum.STATUS405.getCode(), StatusCodeEnum.STATUS405.getMsg());
        }
        String openId = TokenUtils.nothingness(token);
        JiuquanUserModel user = userService.getUserByOpenid(openId);
        if (user == null) {
            return Result.error(StatusCodeEnum.STATUS406.getCode(), StatusCodeEnum.STATUS406.getMsg());
        }
//        用户id
        Integer userId = user.getId();
        //导师id
        Integer mentorId = mentorInfoModel.getId();
        //咨询导师(辅导次数+1)
        String message = mentorInfoService.addCounsellingCount(mentorId, userId);
        return Result.success(message);
    }
    //新增和修改
    @PostMapping("/addAndUpdateMentorInfo")
    public Result addAndUpdateMentorInfo(@RequestBody MentorInfoModel mentorInfoModel) {
        MentorInfoModel mentorInfo = mentorInfoService.getById(mentorInfoModel.getId());
        if (mentorInfo != null) {
            mentorInfoModel.setUpdateTime(CommonHelp.getNowTimestamp());
            mentorInfoService.updateById(mentorInfoModel);
            return Result.success("修改成功");
        } else {
            mentorInfoModel.setCreateTime(CommonHelp.getNowTimestamp());
            mentorInfoService.save(mentorInfoModel);
            return Result.success("添加成功");
        }
    }
    //删除
    @GetMapping("/delMentorInfo")
    public Result delMentorInfo(Integer id) {
        mentorInfoService.removeById(id);
        return Result.success("删除成功");
    }
}
